package com.yz.jdd;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.yz.jdd.base.MyApplication;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.module.launch.view.LaunchActivity;
import com.yz.jdd.module.login.view.LoginActivity;
import com.yz.jdd.module.main.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {
    private ViewPager guideViewPager;   //声明一个ViewPager的实例
    private List<View> guideViews;    //声明一个View的实例数组
    private int[] images = new int[]{R.id.relative1,R.id.relative2,R.id.relative3}; //声明一个图片数组
    private Button startButton;  //声明一个Button实例
    private ImageView[] guideDots;  //声明一个ImageView实例数组
    private RelativeLayout guidViewList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {//页面初始化
        super.onCreate(savedInstanceState);    //页面初始化
        setContentView(R.layout.activity_guidepage); //关联的那个页面
        initViews();
        initDots();
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Context context = view.getContext();
                startActivity(new Intent( GuideActivity.this, LoginActivity.class));
                finish();
            }
        });
        guideViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { //监听guideViewPager的change事件
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d("aaaaaaaaaaaaaa", "eeeeeeeeeeeeee");
                setCurrentDot(position);  //有变动的时候切换小圆点
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }



    void initViews(){   //初始化viewpager
        Log.d("aaaaaaaaaaaaaa", "bbbbbbbbbbbbbb");
        guideViewPager = findViewById(R.id.guide_view_pager);   //将页面的viewpager和实例关联
        startButton = findViewById(R.id.button3);
        guideViews = new ArrayList<View>();  //将三个页面放到guideViews方便管理
        for ( int i = 0;i<images.length;i++) { //循环图片数组
            guidViewList = findViewById(images[i]);
            if (guidViewList.getParent() != null) {
                // 如果有父布局，则先将 childView 从原父布局中移除
                ((ViewGroup) guidViewList.getParent()).removeView(guidViewList);
            }
            guideViews.add(guidViewList);
        }//除了最后一个，因为最后一个要加按钮
        //适配器用于提供数据
        guideViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 3;  //有几个页面
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { //判断view和object是否相等
                return view==object;
            }
            @Override
            public Object instantiateItem(ViewGroup container,int position){  //得到view的方法  container容纳3个页面的容器 // position第几个页面
                Log.d("aaaaaaaaaaaaaa", "cccccccccccccc");
                container.addView(guideViews.get(position)); //将页面加到容器中
                return guideViews.get(position);//将页面加到容器中
            }
            @Override
            public void destroyItem(ViewGroup container,int position,Object o){ //释放view的方法
                container.removeView(guideViews.get(position)); //将容器中的页面释放
            }
        });
    }

    void initDots(){
        Log.d("aaaaaaaaaaaaaa", "dddddddddddd");
        LinearLayout layout = findViewById(R.id.guide_dots); //小圆点的容器
        guideDots = new ImageView[guideViews.size()]; //guideDots实例赋值3个长度的数组
        for (int i = 0; i < guideDots.length; i++) {
            guideDots[i] = (ImageView) layout.getChildAt(i); //循环往数组中添加组件   (ImageView) layout.getChildAt(i) //layout布局中的第几个子组件返回view设成Imageview
        }
        setCurrentDot(0); //选中第1个小圆点
    }
    void setCurrentDot(int pos){
        Log.d("aaaaaaaaaaaaaa", "ffffffffffff");
        for (int i = 0; i < guideDots.length; i++) {
            guideDots[i].setSelected(false);  //将小圆点的状态设成灰
        }
        guideDots[pos].setSelected(true); //将选中的小圆点的状态设成红
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //不再显示GuideActivity
        PreferencesUtils.putBoolean(MyApplication.application, Constants.PRE_SHOW_GUIDE, false);
    }
}